﻿/// <reference path="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" />
/// <reference path="silverlight.js" />
var lightbox$slhost = null;
var lightbox$slversion = '4.0.50401.0';

function lightbox$run()
{
    $('a[rel^=lightbox]')
        .click(lighbox$click);

    $('<div />')
        .attr('id', 'silverlightbox')
        .attr('style', 'position: absolute; top: 0px; left: 0px; right: 0px; height: 0px; z-index:1000;')
        .append('<object id="silverlightboxhost" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">\
                    <param name="source" value="slb/Silverlightbox.xap"/>\
                    <param name="onError" value="lightbox$silverlighterror" />\
                    <param name="onLoad" value="lightbox$silverlightload" />\
                    <param name="background" value="#00000000" />\
                    <param name="windowless" value="true" />\
                    <param name="enableHtmlAccess" value="true" />\
                    <param name="minRuntimeVersion" value="' + lightbox$slversion + '" />\
                    <param name="autoUpgrade" value="true" />\
                    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=' + lightbox$slversion + '" style="text-decoration:none">\
                        <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>\
                    </a>\
                </object>')
        .appendTo(document.body);
}

function lighbox$click(event)
{
    event.preventDefault();

    var rel = $(this).attr('rel');
    var href = $(this).attr('href');
    var rx = /^lightbox(\[(.*)\])?$/i;
    var collection = rx.exec(rel)[2];

    var images = [];
    $('a[rel=' + rel + ']').each(function ()
    {
        images.push({
            uri: $(this).attr('href'),
            title: $(this).attr('title'),
            text: $(this).text()
        });
    });

    lighbox$show(collection, href, images);
}

function lighbox$show(collectionName, current, images)
{
    $('#silverlightbox')
        .css('height', 'auto')
        .css('bottom', '0px');

    lightbox$slhost.Content.coordinator.load(collectionName, current, images);
}

function lighbox$hide(collectionName, current, images)
{
    $('#silverlightbox')
        .css('height', '0px')
        .css('bottom', '');
}

function lightbox$silverlightload(sender, args)
{
    lightbox$slhost = sender.getHost();
}

function lightbox$silverlighterror(sender, args)
{
    var appSource = "";
    if (sender != null && sender != 0)
    {
        appSource = sender.getHost().Source;
    }

    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    if (errorType == "ImageError" || errorType == "MediaError")
    {
        return;
    }

    var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError")
    {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError")
    {
        if (args.lineNumber != 0)
        {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}